(function(window, $) {

    var bid = $('.blog-article').data('id');

    $('#thumb-up').on('click', thumbHandler);
    $('#collect').on('click', collectHandler);

    //点赞
    function thumbHandler (e)  {
        e.preventDefault();

        $.get('/blog/thumbup', {'bid': bid}, function(data) {
            alert(data);
            $('.eux-icon-good-lg').addClass('active');
            $('#thumb-up').off('click', thumbHandler);
        });

        return false;
    }

    //收藏
    function collectHandler (e)  {
        e.preventDefault();

        $.get('/blog/love', {'bid': bid}, function(data) {
            alert(data);
            $('.eux-icon-love-lg').addClass('active');
            $('#collect').off('click', collectHandler);
        });

        return false;
    }


    if ($('.eux-icon-good-lg').hasClass('active')) {
        $('#thumb-up').off('click', thumbHandler);
    }

    if ($('.eux-icon-love-lg').hasClass('active')) {
        $('#collect').off('click', collectHandler);
    }

    //删除文章
    $('#article-delete').click(function(e) {
        e.preventDefault();

        if (confirm('确定要删除这篇文章吗?')) {
            $.get('/blog/delete', {'bid': bid}, function(data) {
                alert(data);
                location.href = '/blog';
            });
        }

        return false;
    });

    //对文章发表评论
    $('.comment-form form').submit(function(e) {
        e.preventDefault();

        var uid = $('.comment-form').data('id');

        var data = {};

        var submitData = $(this).serializeArray();

        for (var i = 0; i < submitData.length; i++) {
            var name = submitData[i].name;
            var value = submitData[i].value;

            data[name] = value;
        }

        data.uid = uid;
        data.bid = bid;

        $.post('/blog/comment', data, function(data) {
            if (data.status == 200) {
                //直接刷新吧,懒得做了。。。
                alert(data.msg);
                location.href = '/blog/article/' + bid;
            } else {
                alert(data.msg);
            }
        });

        return false;
    });


    //评论回复功能
    $('.comment-reply-btn').click(function(e) {
       e.preventDefault();

        var data = $(this).data();
        //唤起回复框
        var formBox = $(this).parents('.comment-content').find('.reply-form');
        formBox.data(data)

        if ($(this).text() == '回复'){
            $(this).text('收起');
        } else {
            $(this).text('回复');
        }

        formBox.slideToggle();

        return false;
    });

    $('.reply-reply-btn').click(function(e) {
        e.preventDefault();

        var data = $(this).data();
        //唤起回复框
        var formBox = $(this).parents('.comment-reply').find('.reply-form');
        formBox.data(data)

        if ($(this).text() == '回复'){
            $(this).text('收起');
        } else {
            $(this).text('回复');
        }

        formBox.slideToggle();

        return false;
    });

    //评论回复请求
    $('.comment-item form').submit(function(e) {
        e.preventDefault();

        var submitData = $(this).serializeArray();

        var data = {
            comment: submitData[0].value
        }


        var uid = $(this).parent().data('uid');
        var cid = $(this).parent().data('cid');

        data.uid = uid;
        data.cid = cid;
        data.bid = bid;

        $.post('/blog/reply', data, function(data) {
           if (data.status == 200) {
               //回复成功
               alert(data.msg);
               location.reload(); //不管了,不想写了。。。
           } else {
               alert(data.msg);
           }
        });

        return false;
    });



})(window, $);
